<template>
	<view class="inputGreybg">
		<image class="uploadImg" src="https://dnsverify.pfmoto.com/appstatic/community/addMedia.png"
			mode="scaleToFill" @click="selectMedia"/>
	</view>
</template>

<script>
	const app = getApp();
	const util = require('@utils/util.js');
	const dialog = require('@utils/dialog.js');

	// #ifdef APP-PLUS
	const PluginModule = uni.requireNativePlugin("PluginModule");
	import { requestAndroidPermission , gotoAppPermissionSetting } from "@/utils/permission.js"
	// #endif
	var _self;
	
	export default {
		props: {
			chooseFileList: {
				type: Array,
				default () {
					return []
				}
			},
			sourceFileList: {
				type: Array,
				default () {
					return []
				}
			},
		},
		data() {
			return {
				
			}
		},
		computed: {

		},
		onLoad() {

		},
		methods: {
			selectMedia(){
				this.$emit('selectMedia')
			},
			//1 视频 2图片
			upload(index, from) {
				console.log(index, from)
				var that = this
				// if (url) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请删除后重新上传'
				// 	})
				// 	return
				// }
				if (index == 3) {
					// #ifdef APP-PLUS
					this.uploadAlbum(index);
					// #endif
					// #ifdef MP-WEIXIN
					this.uploadAlbumWx(index);
					// #endif
				} else if (index == 2) {
					// #ifdef H5
					this.uploadH5();
					// #endif
					// #ifdef APP-PLUS
					this.uploadAlbum(index);
					// #endif
					//#ifdef MP-WEIXIN
					this.uploadAlbumWx();
					// #endif
				} else if (index == 1) {
					// #ifdef H5 ||APP-PLUS
					this.recordNativeMedia();
					// #endif
			
					//#ifdef MP-WEIXIN
					console.log("调用微信拍摄功能")
					if (from == "发动态") {
						console.log('*************来源微信端发动态')
						uni.chooseMedia({
							count: 1,
							mediaType: ['image', 'video'],
							sourceType: ['camera'],
							maxDuration: 30,
							camera: 'back',
							success(res) {
								var type = res.type
								if (that.uploadFileList.length > 1) {
									if (that.uploadFileList[1].mediaType != type) {
										uni.showToast({
											icon: 'none',
											title: '视频或者图片只能选择一种上传'
										})
										return
									}
								}
								console.log(res)
								if (res.tempFiles[0].duration > 30) {
									uni.showToast({
										icon: 'none',
										title: '上传视频不能超过30S'
									})
									return
								}
								// type = res.tempFiles[0].fileType
								// that.uploadFile(res.tempFiles[0].tempFilePath, type, res.tempFiles[0].thumbTempFilePath);
								that.$emit('getChooseFile', res.tempFiles[0].tempFilePath , type , res.tempFiles[0].thumbTempFilePath)
							}
						})
			
			
					} else {
						console.log('*************来源其他')
						uni.chooseImage({
							count: 1,
							sourceType: ['camera'],
							success: (res) => {
								var type = 'image'
								console.log(that.uploadFileList)
								if (that.uploadFileList.length > 1) {
									if (that.uploadFileList[1].mediaType != type) {
										uni.showToast({
											icon: 'none',
											title: '视频或者图片只能选择一种上传'
										})
										return
									}
								}
								// that.uploadFile(res.tempFiles[0].path, type);
								that.$emit('getChooseFile', res.tempFiles[0].path, type)
							},
							fail: (err) => {
								console.log(err, 'err')
							},
						});
					}
					// #endif
				}
			},
			//微信
			uploadAlbumWx() {
				//debugger
				//todo 图片最多限制，视频上传后不能上传图片限制
				var that = this;
				if (this.sourceFileList[0].mediaType == "video") {
					uni.showToast({
						icon: 'none',
						title: '视频或者图片只能选择一种上传'
					})
					return
				}
				uni.chooseMedia({
					count: 10 - this.sourceFileList.length,
					sourceType: ['album'],
					success: (res) => {
						console.log(res)
						// that.tempImg = res.tempFiles[0].thumbTempFilePath
						// this.uploadFile(res.tempFilePaths[0])
						// var type = 'image'
						var type = res.type
						console.log(that.sourceFileList)
						//debugger
						if (that.sourceFileList.length > 1) {
							if (that.sourceFileList[0].mediaType != type) {
								uni.showToast({
									icon: 'none',
									title: '视频或者图片只能选择一种上传'
								})
								return
							}
						}
						if (res.tempFiles[0].duration > 30) {
							uni.showToast({
								icon: 'none',
								title: '上传视频不能超过30S'
							})
							return
						}
						type = res.tempFiles[0].fileType
						//debugger
						// that.uploadFile(res.tempFiles[0].tempFilePath, type, res.tempFiles[0].thumbTempFilePath)
						var list=[]
						for(var j=0;j<res.tempFiles.length;j++){
							list.push(res.tempFiles[j].tempFilePath)
						}
						
						// that.$emit('getChooseFile', res.tempFiles[0].tempFilePath , type , res.tempFiles[0].thumbTempFilePath)
						that.$emit('getChooseFile', list , type , res.tempFiles[0].thumbTempFilePath)
					},
					fail: (err) => {
						console.log(err, 'err')
					},
				});
			},
			//App
			uploadAlbum(index) {
				//todo 图片最多限制，视频上传后不能上传图片限制
				var that = this;
				if (that.sourceFileList.length > 1) {
					if (this.sourceFileList[0].mediaType == "video") {
						uni.showToast({
							icon: 'none',
							title: '视频或者图片只能选择一种上传'
						})
						return
					}
				}
				if (index == 3) {
					uni.chooseImage({
						count: 10-this.sourceFileList.length,
						sourceType: ['album'],
						success: (res) => {
							var type = 'image'
			
							if (that.sourceFileList.length > 1) {
								if (that.sourceFileList[0].mediaType != type) {
									uni.showToast({
										icon: 'none',
										title: '视频或者图片只能选择一种上传'
									})
									return
								}
							}
							// that.uploadFile(res.tempFilePaths[0], type);
							//res..tempFilePaths传给moment
					
							that.$emit('getChooseFile', res.tempFilePaths,type)
						},
						fail: (err) => {
							console.log(err, 'err')
						},
					});
				} else {
					uni.chooseVideo({
						count: 1,
						sourceType: ['album'],
						success: (res) => {
							var type = 'video'
							console.log(res)
							if (that.sourceFileList.length > 1) {
								if (that.sourceFileList[0].mediaType != type) {
									uni.showToast({
										icon: 'none',
										title: '视频或者图片只能选择一种上传'
									})
									return
								}
							}
							if (res.duration > 30) {
								uni.showToast({
									icon: 'none',
									title: '上传视频不能超过30S'
								})
								return
							}
			
							// that.uploadFile(res.tempFilePath, 'video')
							that.$emit('getChooseFile', res.tempFilePath,'video')
						},
						fail: (err) => {
							console.log(err, 'err')
						},
					});
				}
			
			},
			//h5
			uploadH5() {
				uni.chooseFile({
					count: 9,
					// sourceType: ["camera"],
					success: (res) => {
						let fileImg = {};
			
						var type = res.tempFiles[0].type.split('/')[0]
			
						console.log(this.sourceFileList, 'type')
						if (this.sourceFileList.length > 1) {
							if (this.sourceFileList[0].mediaType != type) {
								uni.showToast({
									icon: 'none',
									title: '视频或者图片只能选择一种上传'
								})
								return
							}
						}
						this.uploadFile(res.tempFilePaths[0], type);
						console.log(this.fileList);
					},
					fail: (err) => {
						// #ifdef MP
						uni.getSetting({
							success: (res) => {
								let authStatus = res.authSetting["scope.camera"];
								if (!authStatus) {
									uni.showModal({
										title: "授权失败",
										content: "此操作需要从您的相机获取图片，请在设置界面打开相关权限",
										success: (res) => {
											if (res.confirm) {
												uni.openSetting();
											}
										},
									});
								}
							},
						});
						// #endif
					},
				});
			},
			//调用原生插件录制视频拍摄照片
			recordNativeMedia() {
				const _this = this;
				let platform = uni.getSystemInfoSync().platform; // 获取客户端信息，platform为ios/android
			
				if (plus.os.name == "iOS") {
					// uni.$emit('pauseVideo')
					// 调起拍摄
					_this.startRecored()
				} else {
					console.log('PluginModule')
					const CAMERA = requestAndroidPermission('android.permission.CAMERA')
					const RECORD_AUDIO = requestAndroidPermission('android.permission.RECORD_AUDIO')
					const WRITE_EXTERNAL_STORAGE = requestAndroidPermission('android.permission.WRITE_EXTERNAL_STORAGE')
					if (CAMERA && RECORD_AUDIO && WRITE_EXTERNAL_STORAGE) {
						// 调起拍摄
						_this.startRecored()
					} else {
						console.log('false')
						uni.showModal({
							title: '提示',
							content: "请允许APP使用照相设备、录制音频、存储文件",
							success: function(res) {
								if (res.confirm) {
									gotoAppPermissionSetting()
								}
							}
						});
					}
				}
			},
			startRecored() {
			
				var that = this;
				PluginModule.startRecord({
					"xxx": "xxx",
				}, res => {
					console.log(res)
			
					if (res.result === false) {
						console.log('放弃拍摄')
						return;
					}
					var type = res.isVideo ? 'video' : 'image'
			
					if (that.sourceFileList.length > 1) {
						if (that.sourceFileList[0].mediaType != type) {
							uni.showToast({
								icon: 'none',
								title: '视频或者图片只能选择一种上传'
							})
							return
						}
					}
			
					// that.uploadFile('file://' + res.data, type);
					that.$emit('getChooseFile', 'file://' + res.data,type)
				})
			},
		},
	}
</script>

<style scoped>
	.uploadImg {
		width: 216rpx;
		height: 216rpx;
		display: inline-block;
	}
	
</style>
